package com.mxpio.erp.plm.controller;

import com.mxpio.erp.common.plm.entity.ProjectProblemSort;
import com.mxpio.erp.common.plm.service.ProjectProblemSortService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "ProjectProblemSortController", description = "项目问题分类")
@RestController
@RequestMapping("/erp/plm/pro/problem/sort/")
public class ProjectProblemSortController {

    @Autowired
    private ProjectProblemSortService projectProblemSortService;

    @GetMapping("tree")
    @Operation(summary = "获取问题分类树", description = "获取问题分类树", method = "GET")
    public Result<List<ProjectProblemSort>> list(Criteria criteria) throws UnsupportedEncodingException {
        List<ProjectProblemSort> problemSort = projectProblemSortService.tree(ProjectProblemSort.class, criteria);
        return Result.OK(problemSort);
    }

    @GetMapping("list/{problemCode}")
    @Operation(summary = "根据problemCode获取问题分类", description = "根据problemCode获取问题分类", method = "GET")
    public Result<ProjectProblemSort> getById(@PathVariable(name = "problemCode", required = true) String problemCode) {
        ProjectProblemSort problemSort = projectProblemSortService.getById(ProjectProblemSort.class, problemCode);
        return Result.OK(problemSort);
    }

    @PostMapping("add")
    @Operation(summary = "新增问题分类", description = "新增问题分类", method = "POST")
    public Result<?> add(@RequestBody ProjectProblemSort problemSort) {
       return projectProblemSortService.add(problemSort);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑问题分类", description = "编辑问题分类", method = "PUT")
    public Result<?> edit(@RequestBody ProjectProblemSort problemSort) {
        return projectProblemSortService.updateProblem(problemSort);
    }

    @DeleteMapping("remove/{problemCode}")
    @Operation(summary = "删除问题分类", description = "删除问题分类", method = "DELETE")
    public Result<ProjectProblemSort> remove(@PathVariable(name = "problemCode", required = true) String problemCode) {
        String ids[] = problemCode.split(",");
        for(String key : ids){
            projectProblemSortService.deleteALL(ProjectProblemSort.class, key);
        }
        return Result.OK();
    }


}
